//
//  OrderEvaluationView.m
//  phmall
//
//  Created by admin on 2017/10/9.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "OrderEvaluationView.h"
#define Button_Origin_Tag 100
@interface OrderEvaluationView ()

@property(nonatomic,strong)UIView  *butBack;

@end

@implementation OrderEvaluationView
-(instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor whiteColor];
        
        self.title=[[UILabel alloc]init];
        self.title.text=@"选择评价";
        self.title.font=[UIFont systemFontOfSize:16];
        self.title.textColor=[UIColor colorWithHexString:@"#5a5a5a"];
        [self addSubview:self.title];
        
        
        self.butBack=[[UIView alloc]init];
        [self addSubview:self.butBack];
        
       
        
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(0);
        make.centerY.offset(0);
    }];
    
    [self.butBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).priorityHigh();
        make.left.equalTo(self.title.mas_right).offset(10).priorityHigh();
        make.right.equalTo(self).priorityHigh();
        make.height.equalTo(self);
    }];
    
    while ([self.butBack.subviews lastObject] != nil) {
        [(UIView*)[self.butBack.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
    }
    NSMutableArray *butArr = [NSMutableArray array];
    for (int i=0; i<3; i++) {
        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.butBack addSubview:but];
        switch (i) {
            case 0:
                [but setTitle:@"好评" forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"好评未选中"] forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"好评选中"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"feba03"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"#5a5a5a"] forState:UIControlStateNormal];
                break;
            case 1:
                [but setTitle:@"中评" forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"中评未选中"] forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"中评选中"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"feba03"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"#5a5a5a"] forState:UIControlStateNormal];
                break;
            case 2:
                [but setTitle:@"差评" forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"差评未选中"] forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"差评选中"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"feba03"] forState:UIControlStateSelected];
                [but setTitleColor:[UIColor colorWithHexString:@"#5a5a5a"] forState:UIControlStateNormal];
                break;
            default:
                break;
        }
        but.tag=Button_Origin_Tag+i;
        but.titleLabel.font=[UIFont systemFontOfSize:15];
        [butArr addObject:but];
        [but addTarget:self action:@selector(evaluation:) forControlEvents:UIControlEventTouchUpInside];
        but.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [but layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    }
    
    [butArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:70 leadSpacing:5 tailSpacing:5];
    [butArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.butBack);
        //        UIView *blueView = (UIView *)blueViews[0];
        make.height.mas_equalTo(30);
    }];
//

}
-(void)evaluation:(UIButton*)button{
    //获取点击的是第几个button
    NSInteger index = button.tag - Button_Origin_Tag;
    [self setSelectAtIndex:index];
    
    
    
}
-(void)setSelectAtIndex:(NSInteger)index
{
    //1、先调整自身的视图显示
    for (int i = 0; i < 3; i++) {
        UIButton * bt = [self viewWithTag:i+Button_Origin_Tag];
        
        if (bt.tag-Button_Origin_Tag == index) {
            bt.selected = YES;
            
        }else{
            bt.selected = NO;
        }
    }
    
}

@end

